<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<span style="font-size:18px;">
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" type="text/css" href="purchase/css/bootstrap.min.css" />
    <link rel="stylesheet" type="text/css" href="purchase/css/commonly-used-show.css" />
    <script type="text/javascript" src="purchase/js/jquery.min.js"></script>
    <script type="text/javascript" src="purchase/js/bootstrap.js" ></script>

    <!--验证码与一些正则表达式 -->
    <script type="text/javascript" src="purchase/js/overall.js"></script>

    <!--时间选择 -->
    <link rel="stylesheet" type="text/css" href="purchase/css/bootstrap-datetimepicker.min.css" />
    <script type="text/javascript" src="purchase/js/bootstrap-datetimepicker.js" charset="UTF-8"></script>
    <script type="text/javascript" src="purchase/js/bootstrap-datetimepicker.zh-CN.js" charset="UTF-8"></script>

    <c:if test="${param.id == 1}">
        <title>新增供应商</title>
    </c:if>

    <c:if test="${param.id != 1}">
        <title>修改供应商</title>
    </c:if>



</head>

<body class="mybody">

<c:if test="${user_leve == 1}">
    <jsp:include page="theTopMenuAdmin.jsp" />
</c:if>

<c:if test="${user_leve == 2}">
    <jsp:include page="theTopMenuUser.jsp" />
</c:if>


 <div class="container-fluid">
    <div class="row">


        <div class="col-md-12">
            <div class="col-md-12">
                    <div class="panel panel-primary">
                        <div class="panel-body">
                            <div class="alert alert-success">



                                <div class="container">
                                    <c:if test="${param.id == 1}">
                                        <h3 style="text-align: center">添加供应商信息</h3>
                                    </c:if>

                                    <c:if test="${param.id != 1}">
                                        <h3 style="text-align: center">修改供应商信息</h3>
                                    </c:if>

        <hr style="filter: alpha(opacity=100,finishopacity=0,style=3)" width="100%" color="#6f5999" size="3" />

        <div id="reset_form" class="container">
            <div class="form-group same-line">
                <label ><b style="color: red">*&nbsp</b>供货商编号：</label>
                <input type="text" id="supp_numbe" class="form-control edit" placeholder="请输入供货商编号" maxlength="16">
            </div>

            <div class="form-group same-line-left">
                <label ><b style="color: red">*&nbsp</b>供货商代理人：</label>
                <input type="text" id="user_name" class="form-control edit" placeholder="请输入供货商代理人" maxlength="6">
            </div>

            <div class="form-group same-line-left">
                <label ><b style="color: red">*&nbsp</b>联系电话：</label>
                <input type="text" id="user_photo" class="form-control edit" placeholder="请输入联系电话" maxlength="16">
            </div>

            <div class="form-group same-line">
                <label ><b style="color: red">*&nbsp</b>证件名称：</label>
                <select class="form-control edit" title="请选择" id="certif_name" >
                    <option value="" disabled selected hidden>请选择</option>
                    <option value="居民身份证">居民身份证</option>
                    <option value="护照">护照</option>
                </select>
            </div>

            <div class="form-group same-line-left">
                <label><b style="color: red">*&nbsp</b>证件号码：</label>
                <input type="text" id="certif_number" class="form-control edit" placeholder="请输入证件号码" maxlength="25">
            </div>

            <div class="form-group same-line-left">
                <label ><b style="color: red">*&nbsp</b>邮箱地址：</label>
                <input type="email" id="user_email" class="form-control edit" placeholder="请输入邮箱地址" maxlength="20">
            </div>

            <div class="form-group same-line">
                <label ><b style="color: red">*&nbsp</b>签发时间：</label>
                <div class="controls input-append date form_datetime">
                    <input id="issuance_time" class="form-control edit" size="16" type="text" placeholder="请选择" readonly  />
                    <span class="add-on"><i class="icon-remove"></i></span>
                    <span class="add-on"><i class="icon-th"></i></span>
                </div>
                <input class="form-control edit" type="hidden" />
            </div>

            <div class="form-group same-line-left">
                <label ><b style="color: red">*&nbsp</b>有效期限：</label>
                <div class="controls input-append date form_datetime" >
                    <input id="term_validity" class="form-control edit" size="16" type="text" placeholder="请选择" readonly  />
                    <span class="add-on"><i class="icon-remove"></i></span>
                    <span class="add-on"><i class="icon-th"></i></span>
                </div>
                <input class="form-control edit" type="hidden" />
            </div>

            <div class="form-group same-line-left">
                <label ><b style="color: red">*&nbsp</b>性别：</label>
               <select class="form-control edit" title="请选择" id="user_sex" >
                    <option value="" disabled selected hidden>请选择</option>
                    <option value="男">男</option>
                    <option value="女">女</option>
                </select>
            </div>

            <div class="form-group same-line">
               <label ><b style="color: red">*&nbsp</b>证件图片：</label>
                <div>
                     <img style="float: left" id="head_port" src="purchase/img/img_certificate.png" width="110px" height="90px" >
                    <input type="file" onChange="selectImage(this)" style="width: 190px; float: right; margin-top: 30px; margin-left: 10px;">
                </div>
            </div>


        </div>

        <div style="margin-top: 30px;" class="form-group text-center same-line-clear">
            <input type="button" id="mybut" value="添加" class="btn btn-primary" onclick="addSupplie()">
            <input type="reset" value="重置" class="btn btn-danger interval" onclick="reset()">
        </div>

        <div class="text-center">
            <span id="user_messge" style="font-size:10px; color: red;"></span>
        </div>

</div>



                            </div>
                        </div>
                    </div>
                </div>
        </div>



    </div>

 </div>




<script type="text/javascript">
    var userdt = JSON.parse(sessionStorage.getItem("user"));
    if (userdt.result.userName != null){
        $("#usname").text(userdt.result.userName);
    }else {
        $("#usname").text(userdt.result.userLoginName);
    }
    var suppNumbe;
    var userName;
    var userSex;
    var userPhoto;
    var certifName;
    var certifNumber;
    var userEmail;
    var issuanceTime;
    var termValidity;


    var graphic;
    var imgurl = "";
    var postUrlua;

    //重置文本框
    function reset(){
        var addUser = document.getElementById("reset_form");
        var ipts = addUser.getElementsByTagName("input");
        for (var i = 0; i < ipts.length; i++){
            ipts[i].value = "";
        }
    }

    if ("${param.id}" !== "1"){
        window.onload = function(){
            getSupplie();
        }
        $("#mybut").val("确认修改");
        postUrlua = "${pageContext.request.contextPath}/member/updateSupplier";

    }else {
        postUrlua = "${pageContext.request.contextPath}/member/addSupplier";
    }

    function getSupplie(){
        var postUrl = "${pageContext.request.contextPath}/member/querySupplier";
        var str = {
            "id": "${param.id}"
        };

        $.ajax({
            type:"post",
            url:postUrl,
            headers: {
                "Content-Type":"application/json"
            },
            data:JSON.stringify(str),
            contentType: "application/json; charset=utf-8",
            dataType:"json",
            success:function(data){
                if (data.status === 0){
                    $("#user_messge").text(data.msg);
                }else {
                    $("#supp_numbe").val(data.result.supplierNo);
                    $("#user_name").val(data.result.agentName);
                    $("#user_sex").val(data.result.gender);
                    $("#user_photo").val(data.result.phone);
                    $("#certif_name").val(data.result.certificateName);
                    $("#certif_number").val(data.result.certificateNumber);
                    $("#user_email").val(data.result.email);
                    $("#issuance_time").val(data.result.issuanceTime);
                    $("#term_validity").val(data.result.validPeriod);

                    var url = "${pageContext.request.contextPath}" + data.result.certificatePhoto;
                    $("#head_port").attr("src", url);
                    imgurl = data.result.certificatePhoto;
                }

            },
            error:function(xhr,state,errorThrown){
                //alert("发生网络错误，错误码为：" + xhr.status)
                location.href = "${pageContext.request.contextPath}/error";
            }
        });
    }



    function addSupplie(){
        suppNumbe = $("#supp_numbe").val();
        userName = $("#user_name").val();
        userSex = $("#user_sex").val();
        userPhoto = $("#user_photo").val();
        certifName = $("#certif_name").val();
        certifNumber = $("#certif_number").val();
        userEmail = $("#user_email").val();
        issuanceTime = $("#issuance_time").val();
        termValidity = $("#term_validity").val();


        if ($.isEmptyObject(suppNumbe)){
            $("#user_messge").text("供货商编号不能为空");
            return;
        } else if ($.isEmptyObject(userName)){
            $("#user_messge").text("供货商代理人不能为空");
            return;
        }else if ($.isEmptyObject(userSex)){
            $("#user_messge").text("性别不能为空");
            return;
        }else if ($.isEmptyObject(userPhoto)){
            $("#user_messge").text("联系电话不能为空");
            return;
        }else if ($.isEmptyObject(certifName)){
            $("#user_messge").text("证件名称不能为空");
            return;
        }else if ($.isEmptyObject(certifNumber)){
            $("#user_messge").text("证件号码不能为空");
            return;
        }else if ($.isEmptyObject(userEmail)){
            $("#user_messge").text("邮箱地址不能为空");
            return;
        }else if (!checkEmail(userEmail)){
            $("#user_messge").text("邮箱格式不正确");
            return;
        }else if ($.isEmptyObject(issuanceTime)){
            $("#user_messge").text("签发时间不能为空");
            return;
        }else if ($.isEmptyObject(termValidity)){
            $("#user_messge").text("有效期限不能为空");
            return;
        }else {
            $("#user_messge").text("");
        }

        if (imgurl === ""){
            if (graphic == null){
                $("#user_messge").text("证件图片不能为空");
                return;
            }else {
                $("#user_messge").text("");
            }
            uplImg();
        }else {
            if (graphic != null){
                uplImg();
            }else {
                addSupplieOne();
            }

        }

    }

    function uplImg(){
        var postUrl = "${pageContext.request.contextPath}/uploadFile";
        var formData = new FormData();
        formData.append("userId",userdt.result.createUserId);
        formData.append("pictureUrl",graphic);    //生成一对表单属性
        $.ajax({
            type: "post",           //因为是传输文件，所以必须是post
            url: postUrl,         //对应的后台处理类的地址
            data: formData,
            processData: false,
            contentType: false,
            success: function (data) {
                if (data.status === 0){
                    $("#user_messge").text(data.msg);
                }else {
                    imgurl = data.result.pictureUrl;
                    addSupplieOne();
                }
            },
            error:function(xhr,state,errorThrown){
                //alert("发生网络错误，错误码为：" + xhr.status)
                location.href = "${pageContext.request.contextPath}/error";
            }
        });
    }

    function addSupplieOne(){

        var str = {
            "id": "${param.id}",
            "supplierNo": suppNumbe,
            "agentName": userName,
            "gender": userSex,
            "phone": userPhoto,
            "email": userEmail,
            "certificateName": certifName,
            "certificatePhoto": imgurl,
            "certificateNumber": certifNumber,
            "issuanceTime": issuanceTime,
            "validPeriod": termValidity
        };

        $.ajax({
            type:"post",
            url:postUrlua,
            headers: {
                "Content-Type":"application/json"
            },
            data:JSON.stringify(str),
            contentType: "application/json; charset=utf-8",
            dataType:"json",
            success:function(data){
                if (data.status === 0){
                    $("#user_messge").text(data.msg);
                }else {
                    if ("${user_leve}" === "2"){
                        reset();
                        $("#user_messge").text("添加成功");
                    }else {
                        location.href = "${pageContext.request.contextPath}/supplierManagement";
                    }

                }

            },
            error:function(xhr,state,errorThrown){
                //alert("发生网络错误，错误码为：" + xhr.status)
                location.href = "${pageContext.request.contextPath}/error";
            }
        });
    }



    function selectImage(file) {
        if (!file.files || !file.files[0]) {
            return;
        }
        var reader = new FileReader();
        reader.onload = function (evt) {
            document.getElementById("head_port").src = evt.target.result;
        }
        //通过文件流将文件转换成Base64字符串
        reader.readAsDataURL(file.files[0]);
        graphic = file.files[0];
    }


    //加载时间控件
    $('.form_datetime').datetimepicker({
        language: 'zh-CN', //默认语言
        format: "yyyy-mm-dd",//默认选择格式
        autoclose: true,//选中自动关闭
        todayBtn: true,//显示今日按钮
        startView: 2, //设置只显示到月份
        minView: 2

    });

</script>



</body>
</html>
</span>